package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Standardlevel;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("StandardlevelController")
public class StandardlevelController extends CmiEmaController {

	@RequestMapping("/listStandardlevels")
	public ModelAndView listStandardlevels(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = standardlevelPaginationService.updatePagination(request, pagination);
		Set<Standardlevel> standardlevels = standardlevelPaginationService.getPaginationStandardlevels(pagination);

		mav.addObject("standardlevels", standardlevels);
		mav.addObject("entityName", "standardlevel");
		mav.addObject("pagination", pagination);
		mav.setViewName("standardlevel/listStandardlevels");
		return mav;
	}

	public String listStandardlevels() {
		return "redirect:/listStandardlevels";
	}

	@RequestMapping("/viewStandardlevel")
	public ModelAndView viewStandardlevel(@RequestParam Integer standardlevelId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("standardlevel", standardlevelDAO.findStandardlevelByPrimaryKey(standardlevelId));
		mav.setViewName("standardlevel/viewStandardlevel");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteStandardlevels")
	public ModelAndView confirmDeleteStandardlevels(
			@RequestParam(value = "deleteStandardlevelsIds", required = true) Set<Integer> deleteStandardlevelsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Standardlevel> deleteStandardlevels = standardlevelDAO.findStandardlevelsByIds(deleteStandardlevelsIds);
		Set<Standardlevel> referencedStandardlevels = standardlevelService
				.loadReferenceStandardlevels(deleteStandardlevels);
		boolean hasReferenced;
		if (referencedStandardlevels.size() == 0) {
			mav.addObject("deleteEntitys", deleteStandardlevels);
			hasReferenced = false;
		} else {
			deleteStandardlevels.removeAll(referencedStandardlevels);
			mav.addObject("referencedEntitys", referencedStandardlevels);
			mav.addObject("deleteEntitys", deleteStandardlevels);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Standardlevel");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listStandardlevels");
		return mav;
	}

	@RequestMapping("/deleteStandardlevel")
	public String deleteStandardlevel(@RequestParam Integer standardlevelId) {
		Standardlevel standardlevel = standardlevelDAO.findStandardlevelByPrimaryKey(standardlevelId);
		standardlevelService.deleteStandardlevel(standardlevel);
		return "forward:/listStandardlevels";
	}

	@RequestMapping("/confirmDeleteStandardlevel")
	public ModelAndView confirmDeleteStandardlevel(@RequestParam Integer standardlevelId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("standardlevel", standardlevelDAO.findStandardlevelByPrimaryKey(standardlevelId));

		mav.setViewName("standardlevel/deleteStandardlevel");

		return mav;
	}

	@RequestMapping("/deleteStandardlevels")
	public String deleteStandardlevels(
			@RequestParam(value = "deleteStandardlevelsId", required = true) Set<Integer> deleteStandardlevelsId) {
		standardlevelService.deleteStandardlevels(deleteStandardlevelsId);
		return "forward:/listStandardlevels";
	}

	@RequestMapping("/editStandardlevel")
	public ModelAndView editStandardlevel(@RequestParam Integer standardlevelId) {

		ModelAndView mav = new ModelAndView();

		Standardlevel standardlevel = standardlevelDAO.findStandardlevelByPrimaryKey(standardlevelId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("standardlevel", standardlevel);

		// 把所有的引用standardlevel对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardlevelService.loadStandardlevelObjectReferencedSet(standardlevel));

		// 将编辑页面standardlevel添加到mav中
		mav.setViewName("standardlevel/editStandardlevel");
		return mav;
	}

	@RequestMapping("/newStandardlevel")
	public ModelAndView newStandardlevel() {
		ModelAndView mav = new ModelAndView();
		Standardlevel standardlevel = new Standardlevel();

		mav.addObject("standardlevel", standardlevel);

		// 把所有的引用standardlevel对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardlevelService.loadStandardlevelObjectReferencedSet(standardlevel));

		mav.addObject("newFlag", true);
		mav.setViewName("standardlevel/editStandardlevel");

		return mav;
	}

	@RequestMapping("/saveStandardlevel")
	public ModelAndView saveStandardlevel(@ModelAttribute Standardlevel standardlevel) {

		ModelAndView mav = new ModelAndView();

		Standardlevel existingStandardlevel = CmiHibernateProxyUtil
				.deproxy(standardlevelDAO.findStandardlevelByPhysicalPrimaryKey(standardlevel.getName()));

		// 新建记录
		if (standardlevel.getId() == null) {
			if (standardlevel.equals(existingStandardlevel)) {

				mav.addAllObjects(standardlevelService.loadStandardlevelClassReferencedSet(standardlevel));
				// mav = editContinue(mav, standardlevel);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standardlevel/editStandardlevel");
			} else {
				standardlevelService.saveStandardlevel(standardlevel);
				mav.setViewName("forward:/listStandardlevels");
			}
		}
		// 编辑记录
		else {
			Standardlevel originStandardlevel = CmiHibernateProxyUtil
					.deproxy(standardlevelDAO.findStandardlevelByPrimaryKey(standardlevel.getId()));
			if (!standardlevel.equals(originStandardlevel) && standardlevel.equals(existingStandardlevel)) {
				mav.addAllObjects(standardlevelService.loadStandardlevelClassReferencedSet(standardlevel));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standardlevel/editStandardlevel");
			} else {
				standardlevelService.saveStandardlevel(standardlevel);
				mav.setViewName("forward:/listStandardlevels");
			}
		}
		return mav;
	}

	@RequestMapping("/standardlevelController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
